//
//  IKEcgCheckView.m
//  Ikinloop-demo
//
//  Created by Li Shen on 2021/4/16.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "IKEcgCheckView.h"
#import "IKCircleView.h"
#import "IKAudioManager.h"
#import "ECGChartView.h"

@interface IKEcgCheckView ()

@property (nonatomic, strong) IKCircleView *circleView;
//@property (nonatomic, strong) IKEcgChartView *ecgView;
@property (nonatomic, strong) ECGChartView *ecgView;
@property (nonatomic, assign) EcgState state;
@property (nonatomic, strong) IKAudioManager *audioManager;
@property (nonatomic, assign) NSInteger checkTime;
@property (nonatomic, assign) BOOL isStartEcgData;

@property (nonatomic, strong) UIButton *testBtn;

@end
@implementation IKEcgCheckView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.audioManager = [[IKAudioManager alloc] init];

    self.circleView = [[IKCircleView alloc] init];
    float height = [IKPublicUtil mm2pt:40];
    self.ecgView = [[ECGChartView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, height)];
    self.ecgView.bgColor = [UIColor whiteColor];
    self.ecgView.gridColor = COLOR_ECG_GRID_LINE;
    self.ecgView.smallgridColor = COLOR_ECG_GRID_LINE;
    self.ecgView.ecgColor = ECG_COLOR_GREEN;
    [self.ecgView IKEcgDrawinit];
    
    [self addSubview:self.circleView];
    [self addSubview:self.ecgView];
    self.isStartEcgData = false;
    self.checkTime = 40;
}

- (void)IkEcgCheckViewInit{
    self.isStartEcgData = false;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.ecgView.mas_top);
        make.width.equalTo(self);
        make.top.equalTo(self);
    }];
    float height = [IKPublicUtil mm2pt:40];
    [self.ecgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.width.equalTo(self);
        make.height.equalTo(@(height));
        
    }];
    

}

- (void)IKSetCheckTime:(NSInteger)checkTime{
    _checkTime = checkTime;
    [self.circleView setDuration:checkTime];
}

- (void)IKSetEcgCheckState:(EcgState)state value:(id)value{
    self.state = state;
    if(EcgStateBLEOpened == state){
        [self.audioManager playPressBtn];
        [self.circleView setToastState:INTERNATIONAL(@"string_press_power_btn")];
        
    }else if(EcgStateBLEConnectSuccess == state){
        [self.audioManager playHandTip];
        [self.circleView setToastState:INTERNATIONAL(@"string_touch_metal")];
        
    }else if(EcgStateMachineLowPower == state){
        [self.audioManager playLowPower];
        [self.circleView setToastState:INTERNATIONAL(@"string_lowpower")];
        
    }else if(EcgStateBLENotOpen == state){
        [self handleBleNotOpen];
        
    }else if(EcgStateBLEDisconnect == state){
        self.isStartEcgData = false;
        [self.audioManager playPressBtn];
        [self.circleView setToastState:INTERNATIONAL(@"string_press_power_btn")];
    }else if(EcgStateHeartValue == state){
        [self.circleView setHeartValue:value];
    }else if(EcgStateShake == state){
        [self.audioManager playShake];
        [self.circleView setToastState:INTERNATIONAL(@"string_too_big_noise")];
    }else if(EcgStateTime == state){
        NSInteger time = ((NSString*)value).intValue;
        double progress = ((self.checkTime - time + 1) * 1.0 / self.checkTime * 1.0 ) *100;
        [self.circleView setProgress:(NSInteger)progress];
    }else if(EcgStateError == state){
        self.isStartEcgData = false;
        [self.audioManager playUnusable];
        [self.circleView setToastState:INTERNATIONAL(@"string_nosie_big_test_agian")];
    }else if(EcgStateBLEUnauthorized == state){
        self.isStartEcgData = false;
        [self.circleView setToastState:@"未获取蓝牙权限,请去设置中开启"];
    }
}

- (void)IKECGDrawData:(int)ecgdata{
    if(!self.isStartEcgData){
        [self.audioManager playStart];
        self.isStartEcgData = true;
    }
    [self.ecgView IKECGDrawData:ecgdata];
}

- (void)IKResetEcgCheckView{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.circleView resetUI];
        [self.ecgView IKECGDrawUninit];
        [self.circleView setToastState:INTERNATIONAL(@"string_press_power_btn")];
    });
}

- (void)IKECGSetDetectOver{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.audioManager playStop];
        [self.circleView resetUI];
        [self.ecgView IKECGDrawUninit];
        [self.circleView setToastState:INTERNATIONAL(@"string_press_power_btn")];
    });
}

- (void)IkEcgCheckViewUnInit{
    [self.audioManager stopAllVoice];
}

//处理蓝牙未开启
- (void)handleBleNotOpen{
    [self.audioManager playNoBle];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.circleView setToastState:INTERNATIONAL(@"string_open_ble")];
        [self.circleView resetUI];
        [self.ecgView IKECGDrawUninit];
    });
}
@end
